package com.talent.jiwen.my;

import android.content.Context;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.content.ContextCompat;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.TextView;

import com.talent.jiwen.R;
import com.talent.jiwen.base.BaseActivity;
import com.talent.jiwen.teacher.CourseAllListFragment;
import com.talent.jiwen.teacher.CourseListFragment;
import com.talent.jiwen.teacher.CourseWaitListFragment;

import net.lucode.hackware.magicindicator.MagicIndicator;
import net.lucode.hackware.magicindicator.ViewPagerHelper;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.CommonNavigator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.CommonNavigatorAdapter;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerIndicator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerTitleView;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.titles.CommonPagerTitleView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

public class CouponActivity extends BaseActivity {
    @BindView(R.id.indicator)
    MagicIndicator indicator;
    @BindView(R.id.viewPager)
    ViewPager viewPager;

    private List<Fragment> fragmentList = new ArrayList<>();
    private List<String> titleList = new ArrayList<>();

    private static final int STATUS_NO_USE = 0;//0未使用
    private static final int STATUS_ALREADY_USE = 1;//1已使用
    private static final int STATUS_OUT_OF_TIME = 2;//2已过期

    @Override
    protected int provideContentViewId() {
        return R.layout.activity_coupon;
    }

    @Override
    public String titleName() {
        return "优惠券";
    }

    @Override
    protected void initData() {
        titleList.clear();
        titleList.add(getString(R.string.coupon_no_use));
        titleList.add(getString(R.string.coupon_used));
        titleList.add(getString(R.string.coupon_out_of_time));

        fragmentList.clear();
        fragmentList.add(CouponListFragment.newInstance(STATUS_NO_USE));
        fragmentList.add(CouponListFragment.newInstance(STATUS_ALREADY_USE));
        fragmentList.add(CouponListFragment.newInstance(STATUS_OUT_OF_TIME));

        CommonNavigator commonNavigator = new CommonNavigator(this);
        commonNavigator.setAdjustMode(true);
        commonNavigator.setAdapter(new CommonNavigatorAdapter() {

            @Override
            public int getCount() {
                return titleList == null ? 0 : titleList.size();
            }

            @Override
            public IPagerTitleView getTitleView(Context context, final int index) {

                CommonPagerTitleView commonPagerTitleView = new CommonPagerTitleView(context);
                commonPagerTitleView.setContentView(R.layout.item_indicator_coourse);

                final View indicator = (View) commonPagerTitleView.findViewById(R.id.indicator);
                final TextView text = (TextView)commonPagerTitleView.findViewById(R.id.text);

                String title = titleList.get(index);
                text.setText(title);

                commonPagerTitleView.setOnPagerTitleChangeListener(new CommonPagerTitleView.OnPagerTitleChangeListener() {
                    @Override
                    public void onSelected(int i, int i1) {
                        indicator.setVisibility(View.VISIBLE);
                        text.setTextColor(ContextCompat.getColor(CouponActivity.this , R.color.common_app_color));
                    }

                    @Override
                    public void onDeselected(int i, int i1) {
                        indicator.setVisibility(View.GONE);
                        text.setTextColor(ContextCompat.getColor(CouponActivity.this , R.color.text_gray_9));
                    }

                    @Override
                    public void onLeave(int i, int i1, float v, boolean b) {

                    }

                    @Override
                    public void onEnter(int i, int i1, float v, boolean b) {

                    }
                });
                commonPagerTitleView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        viewPager.setCurrentItem(index);
                    }
                });

                return commonPagerTitleView;
            }

            @Override
            public IPagerIndicator getIndicator(Context context) {
                return null;
            }
        });
        indicator.setNavigator(commonNavigator);

        viewPager.setAdapter(new FragmentStatePagerAdapter(getSupportFragmentManager()) {
            @Override
            public Fragment getItem(int i) {
                return fragmentList.get(i);
            }

            @Override
            public int getCount() {
                return titleList.size();
            }

            @Nullable
            @Override
            public CharSequence getPageTitle(int position) {
                return titleList.get(position);
            }
        });

        ViewPagerHelper.bind(indicator, viewPager);
    }
}
